package org.ddr.control;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.catalina.Session;
import org.ddr.member.MemberVO;
import org.ddr.member.dao.MemberDAO;

/**
 * Servlet implementation class MemberControl
 */
@WebServlet("/member/*")
public class MemberControl extends ControlServlet {
	private static final long serialVersionUID = 1L;

	/**
	 * @see HttpServlet#HttpServlet()
	 */
	public MemberControl() {
		super();
		// TODO Auto-generated constructor stub
	}

	public void create(HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		
		request.setCharacterEncoding("UTF-8");
		HttpSession session = request.getSession();
		
		MemberVO vo = new MemberVO();
		vo.setUserid(request.getParameter("id"));
		vo.setPw(request.getParameter("pw"));
		vo.setSex(request.getParameter("sex"));
		vo.setName(request.getParameter("name"));
		vo.setAge(Integer.parseInt(request.getParameter("age")));
		
		if(session.getAttribute("str").equals(request.getParameter("key"))){
						
			MemberDAO.instance.registUser(vo);
		}

		response.sendRedirect("http://localhost/ws4/member/login");
	}

	public void update(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {

	}

	public void delete(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
	}
	
	public void regist(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		RequestDispatcher dispatcher = request
				.getRequestDispatcher("/WEB-INF/jsp/member/regist.jsp");
		dispatcher.forward(request, response);	
	}
	
	public void loginview(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		RequestDispatcher dispatcher = request
				.getRequestDispatcher("/WEB-INF/jsp/member/login.jsp");
		dispatcher.forward(request, response);
	}
	
	public void login(HttpServletRequest request, HttpServletResponse response)
			throws Exception {		

		List<MemberVO> list = new ArrayList<MemberVO>();
		
		if(request.getParameter("id") == "" || request.getParameter("id").length() <= 0){
			HttpSession session = request.getSession();
			session.setAttribute("Login", request.getParameter("id"));
			list = MemberDAO.instance.getUser(request.getParameter("id"),
						request.getParameter("pw"));
				response.sendRedirect("http://localhost/ws4/board/list");
				response.sendRedirect("http://localhost/ws4/member/loginview");
			}
		else{
			response.getWriter().println("<script>alert('aaaaa'); </script>");
//			response.sendRedirect("http://localhost/ws4/member/loginview");
		}
	}

}
